using NL.AdreesableAsset;
using UnityEngine;

public class ParticleChangeTexture : MonoBehaviour
{
    [SerializeField] private Sprite _sprite;

    private ParticleSystem _particleSystem;
    private ParticleSystemRenderer _particleSystemRenderer;
    private static readonly int Property = Shader.PropertyToID("_BaseMap");

    private void Awake()
    {
        _particleSystem = GetComponentInChildren<ParticleSystem>();
        if (_particleSystem)
        {
            DisableParticle();
            _particleSystemRenderer = _particleSystem.GetComponent<ParticleSystemRenderer>();
            if (_particleSystemRenderer && _sprite)
            {
                ChangeTexture(_sprite);
            }
        }
    }

    private void OnDisable()
    {
        DisableParticle();
    }

    public void ChangeTexture(string textureUrl)
    {
        if (_particleSystemRenderer == null)
        {
            return;
        }

        AA.LOAD<Sprite>(textureUrl, (s, sprite) =>
        {
            ChangeTexture(sprite);
        });
    }

    private void ChangeTexture(Sprite sprite)
    {
        _particleSystem.gameObject.SetActive(true);
        _particleSystemRenderer.material.SetTexture(Property, sprite.texture);
    }

    private void DisableParticle()
    {
        if (_particleSystem != null)
        {
            _particleSystem.gameObject.SetActive(false);
        }
    }
}